<?php

namespace app\admin\validate\factor;

use app\admin\model\factor\FactorReview;
use think\Validate;

class Authority extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
        'factor_id' => 'require|checkFactorId',
        'product_name' => 'require',
        'product_specification' => 'require',
        'product_function' => 'require',
        'auth_date_start' => 'require|checkDate',
        'auth_date_end' => 'require|checkDate',
//        'product_introduction' => 'require',
        'quantification_method' => 'require|in:1,2',
        'file_base64' => 'require',
    ];

    /**
     * 提示消息
     */
    protected $message = [
        'factor_id.require' => '请选择碳排放数据库',
        'product_name.require' => '请填写产品名称',
        'product_specification.require' => '请填写产品规格型号',
        'product_function.require' => '请填写产品功能',
        'product_image.require' => '请上传产品图片',
        'quantification_method.require' => '请选择量化方法',
        'quantification_method.in' => '请选择量化方法',
        'auth_date_start.require' => '请填写数据采集时间',
        'auth_date_end.require' => '请填写数据采集时间',
        'file_base64.require' => '请上传文件',
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => [],
        'edit' => [],

        'authorityFactor' => [
            'product_name',
            'product_specification',
            'product_function',
//            'product_image',
            'quantification_method',
            'factor_id'
        ],
        'factorAuthorityAuthDetail' => ['factor_id'],
        'cancelAuthorityAuth' => ['factor_id'],

    ];

    public function checkFactorId($value, $rule, $data)
    {
        $factor = \app\admin\model\factor\CarbonFactor::where('id', $value)->find();
        if (!$factor) {
            return '数据不存在';
        }
        if ($this->currentScene == 'authorityFactor' ) {
            if ($factor->is_authority == 1) {
                return '该数据已权威认证';
            }
            if ($factor->is_account != 1) {
                return '该数据未核算认证不能进行权威认证';
            }
            if ($factor->product_review != 1) {
//                return '该数据未上架审核不能进行权威认证';
            }

            $lastAuth = $factor->getLastAuthorityAuthAttr();
            if ($lastAuth) {
                if ($lastAuth->status == 0) {
                    return '数据权威认证审核中，请勿重复提交';
                } else if ($lastAuth->status == 1) {
                    return '该数据已权威认证';
                }
            }
        }

        return true;
    }

    public function checkDate($value, $rule, $data)
    {
        if (strtotime($value) === false) {
            return '时间格式不正确';
        }
        return true;
    }

}
